home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-24
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: Pointers to structures
- Date: 3 Jan 1996 22:12:30 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4cev0e$1dhu@news.gate.net>
- References: <4cc9r4$m26@armitage.cyberspace.com> <4ccpgv$qpl@ixnews8.ix.netcom.com> <4cer8r$1v44@news.gate.net>
- NNTP-Posting-Host: pslfl2-39.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4cer8r$1v44@news.gate.net>, bhutto@gate.net (William Hutto) wrote:
- >icarus@loomis (Tel Janin Aellinsar) wrote:
- >
- >>Berserker Dragon, Knights of the Cosmos icarus@BERKSHIRE.NET
- >
- >> I'm having trouble using a pointer to a struct. The idea is to
- >> have a struct get filled by a function, which gets the address
- >> and everything via function(struct type*). Very straightforward.
- >> However, if I try to CHANGE anything in the struct, it just goes
- >> back when the function is over. So, let's pretend this imaginary
- >> structure is what I'm using:
- >>
- >> struct st1 {
- >> char *name;
- >> int yadda;
- >> };
- >>
- >> And the function is:
- >>
- >> fn1(struct st1 *st)
- >
- >If you want to assigned values to be retained, this needs to be double
- >indirection.
- >
- > void fn1(struct st1 **st)
- >
- >> {
- >> st = (struct st1*)malloc(sizeof(struct st1*));
- > ^^^^^^^^^^^
- >This allocates enough space for a pointer, not your structure. Nor does it
- >check validity before usage. This should be:
- >
- > if((*st=malloc(sizeof(struct st1))==NULL) {
- > /*error*/
- > }
- >
- >Also note the indirection: *st
- >
- >st points to the pointer that you pass to this function. That's where
- >malloc()'s returned value is going.
- >
- >> st->name = (char*)malloc(16);
- >
- > if((*st->name=malloc(16))==NULL) {
- > /*error*/
- > }
- >
- >> strcpy(st->name,"Test");
- >
- > strcpy(*st->name,"Test");
- >
- >> st->yadda = 100;
- >
- > *st->yadda = 100;
- >
- >> }
- >
- >You must call this function with the address of a pointer of struct type.
- >
- >func()
- >{
- >struct st1 *stptr;
- >
- > fn1(&stptr);
- > printf("%s, %d\n",stptr->name,stptr->yadda);
- >
- >}
-
- I should have compiled this before posting it. I wanted to post an
- additional way of doing this anyway, which should be easier. The indirect
- member of (->) operator has higher precedence than the indirection (*)
- operator, therefore when accessing the members of a structure in this way you
- need to force the compiler to dereference the pointer to the pointer first:
-
- (*st)->name
-
- The easier way to do this is just to return a pointer to a structure:
-
- func()
- {
- struct st1 *stptr;
-
- stptr=fn1();
- printf("%s, %d\n",stptr->name,stptr->yadda);
-
- }
-
- struct st1 *fn1(void)
- {
- struct st1 *st;
-
- if((st=malloc(sizeof(struct st1)))==NULL) {
- /*error*/
- }
- if((st->name=malloc(16))==NULL) {
- /*error*/
- }
- strcpy(st->name,"Test");
- st->yadda = 100;
- return st;
- }
-
- This accomplishes the same result, but is less convoluted.
-
- Bill
-
-
-
-
- "Whatcha got on?...Your mind?"
-